

import {
  registerDecorator,
  ValidationOptions,
  ValidationArguments,
} from 'class-validator';
const reg = /^[1-9]\d*$/;
/**
 * 此自定义校验用于Get的请求。
 */
export function IsPositvieNumber(validationOptions?: ValidationOptions) {
  return function (object: object, propertyName: string) {
    registerDecorator({
      name: 'IsPositvieNumber',
      target: object.constructor,
      propertyName: propertyName,
      constraints: [],
      options: validationOptions,
      validator: {
        validate(value: any, args: ValidationArguments) {
          // console.log(value, reg.test(value));
          return reg.test(value);
        },
      },
    });
  };
}

/* import {
  registerDecorator,
  ValidationOptions,
  ValidationArguments,
} from 'class-validator';
function ex(str) {
  return /^([1-9][0-9]*){1,3}$/.test(str);
}
//  目的： 有些需要重复多次的，可以简化在这里 ,比如：判断是否为非0正整数正则
export function IsNotZeroPositiveNumber(validationOptions?: ValidationOptions) {
  // object : 要验证的对象
  //  propertyName  : 该对象中验证的字段 
   
  return function (object: Object, propertyName: string) {
    registerDecorator({
      name: 'IsNotZeroPositiveNumber',
      target: object.constructor,
      propertyName: propertyName,

      //   constraints: [property],
      options: validationOptions,
      validator: {
        validate(value: any, args: ValidationArguments) {
          console.log('value', value);
          console.log('value', value);
          console.log(' ex(value)', ex(value));
          return ex(value);
        },
      },
    });
  };
}
 */
